Function Handles

Motivating example

Consider the following problem: we wish to write a Matlab function that estimates the value of the derivative of a given function at a specific point (or points). We'll call this Matlab function derivative and it will take two parameters:

We know from calculus that we can estimate the derivative of a function f at a value x by computing the slope of the line that goes through (x, f(x)) and a nearby point (x + h, f(x + h)) (for some small value of h).

The code for our derivative function is as follows:

function deriv = derivative(fctn, val)
h = 0.0000001;
deriv = (fctn(val + h) - fctn(val))/h;

Now suppose we want to estimate the derivative of cos(x) at 0.4 using our derivative function, so we enter the following in the Command Window:

>> value = 0.4;
>> deriv_at_value = derivative(cos, value)  % will this work??

When Matlab runs this code and sees the names "cos" and "value" that are being passed to the derivative function, how is it supposed to know that "cos" is a function and "value" is a variable containing numbers? The answer is, as we've written it, it doesn't. We need a special way to tell Matlab that a name we give it is a function (and not a "regular" variable). The way we do that is by creating a function handle.

Function handles

A function handle is created by putting an @ symbol before a name. For example, @cos creates a function handle that is "connected to" the cos function. When you pass @cos as a parameter to a function, you are telling Matlab that what you are giving it is a function (and can be treated like one). We can now correctly use our derivative function:

>> value = 0.4;
>> deriv_at_value = derivative(@cos, value)  % correct

Anonymous functions

An anonymous function is a way to define a function and create a function handle in one line, without having to define the function in a separate file. This is especially useful if the function we want is very simple, e.g., has only one input parameter and requires only one line of code to compute its result.

For example, suppose we wanted to estimate the derivative of 2x2 - 3 cos(x) at x = 4. We could define a separate function (in myFctn.m):

function result = myFctn(x)
result = 2*x.^2 - 3*cos(x);

and then in the Command Window do:

>> deriv_at_4 = derivative(@myFctn, 4)

Alternatively, we can do this without defining a separate function file:

>> deriv_at_4 = derivative(@(x)2*x.^2 - 3*cos(x), 4)

In the anonymous function @(x)2*x.^2 - 3*cos(x)

We can even assign a variable name to an anonymous function and use that name like a function:

>> anonFctn = @(x)2*x.^2 - 3*cos(x);
>> y4 = anonFctn(4)                     % calculate the value of the function at x = 4
>> deriv_at_4 = derivative(anonFctn, 4) % estimate the value of the derivative at x = 4

Note that when we assign a variable name to an anonymous function (or function handle) that we don't include the @ when using that name to pass the function as a parameter (because the @ is already included in the variable's definition).